<template>
  <section class="todoapp">
    <todo-header @hadd="hadd"></todo-header>

    <todo-main :list="showlist" @hdel="hdel" @hchange="hchange"></todo-main>

    <todo-footer :list="showlist" @hclear="hclear" @hxuan="hxuan"></todo-footer>
  </section>
</template>

<script>
import TodoFooter from "./components/TodoFooter.vue";
import TodoHeader from "./components/TodoHeader";
import TodoMain from "./components/TodoMain.vue";

export default {
  components: { TodoFooter, TodoHeader, TodoMain },
  data() {
    return {
      type: "all",
      list:JSON.parse(localStorage.getItem('list')),
    };
  },
  methods: {
    hdel(id) {
      let index = this.list.findIndex((itme) => itme.id === id);
      this.list.splice(index, 1);
    },
    hchange(id) {
      let itme = this.list.find((itme) => itme.id === id);
      itme.isDone = !itme.isDone;
    },
    hadd(name) {
      this.list.push({
        id: Date.now(),
        name: name,
        isDone: false,
      });
    },
    hclear() {
      this.list = this.list.filter((itme) => !itme.isDone);
    },
    hxuan(type) {
      this.type = type;
    },
  },
  computed: {
    showlist() {
      if (this.type === "yes") {
        return this.list.filter((itme) => itme.isDone);
      } else if (this.type === "on") {
        return this.list.filter((itme) => !itme.isDone);
      } else {
        return this.list;
      }
    },
  },
  watch: {
    list: {
      handler() {
        localStorage.setItem("list", JSON.stringify(this.list));
      },
      deep: true,
    },
  },
};
</script>

<style></style>
